package com.dg11185.nearshop.view.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.nearshop.R;
import com.baidu.location.BDLocation;
import com.dg11185.libs.imageloader.core.ImageLoader;
import com.dg11185.nearshop.activity.shop.ShopDetailActivity;
import com.dg11185.nearshop.app.ConvertUnit;
import com.dg11185.nearshop.app.DefaultFactory;
import com.dg11185.nearshop.db.entity.Merchant;
import com.dg11185.nearshop.view.adapter.ShopMainAdapter.ViewHolder;
/**
 * 
 * @author：Daibo    
 * @time：2014-6-30 上午8:57:00
 */
public class ShopBrowseAdapter extends BaseAdapter {

    private Context context = null;
    
    private LayoutInflater inflater = null;

    private List<Merchant> shops = null;

    private BDLocation location = null;

    /**
     * 构造函数
     * 
     * @param context
     * @param shops
     *            需要保证数据被初始化，如果使用set函数初始化BaseAdapter可能会报空指针；
     */
    public ShopBrowseAdapter(Context context, List<Merchant> shops) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.shops = shops;
    }

    public void setShops(List<Merchant> shops) {
        this.shops = shops;
    }

    public void setLocation(BDLocation location) {
        this.location = location;
    }
    @Override
    public int getCount() {
        return shops.size();
    }

    @Override
    public Object getItem(int position) {
        return shops.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Merchant unit = shops.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.shop_main_item, parent, false);

            holder.image = (ImageView) convertView.findViewById(R.id.shop_main_image);
            holder.name = (TextView) convertView.findViewById(R.id.shop_main_name);
            holder.rate = (RatingBar) convertView.findViewById(R.id.shop_main_ratebar);
            holder.category = (TextView) convertView.findViewById(R.id.shop_main_category);
            holder.privilege = (TextView) convertView.findViewById(R.id.shop_main_privilege);
            holder.distance = (TextView) convertView.findViewById(R.id.shop_main_distance);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(unit.merchantName);
        ImageLoader.getInstance().displayImage(unit.midPhotoUrl, holder.image, DefaultFactory.getImageoptions());
        if (!"".equals(unit.avgRating)) {
            holder.rate.setRating(Float.parseFloat(unit.avgRating));
        } else {
            holder.rate.setRating(3.8f);
        }

        if (location != null && !"".equals(unit.latitude) && !"".equals(unit.longitude)) {
            
            holder.distance.setText(ConvertUnit.getDistance(location.getLatitude()+"", location.getLongitude()+"",
                    unit.latitude, unit.longitude));
        }
        holder.category.setText(unit.areaName + " " +unit.industryName);
        holder.privilege.setText(unit.discount);
        convertView.setOnClickListener(new ClickListener(position));
        return convertView;
    }

    private class ClickListener implements OnClickListener{

        int position = 0;
        public ClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ShopDetailActivity.class);
            intent.putExtra("merchant_id", shops.get(position).merchantId);
            context.startActivity(intent);
        }
        
    }
}
